package com.prp.gwt.client.user.profile;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.prp.gwt.client.ClientUtility;
import com.prp.gwt.client.Host;
import com.prp.gwt.client.user.UserBaseService;
import com.prp.gwt.client.user.UserBaseServiceAsync;
import com.prp.gwt.client.vo.User;
import com.prp.gwt.client.widgets.TopPanel;
import com.prp.gwt.client.widgets.user.profile.ProfileView;

public class ProfileListener {

	private static ProfileListener instance;
	private ProfileServiceAsync service = GWT.create(ProfileService.class);
	private UserBaseServiceAsync baseService = GWT
			.create(UserBaseService.class);
	public User user;
	AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>(){

		@Override
		public void onFailure(Throwable caught) {
			ClientUtility.handleException(caught);
		}

		@Override
		public void onSuccess(Boolean result) {
			refreshUser(-1);
		}};
		
	public void getUser(int id) {
		if (instance.user == null || id != -1)
			instance.refreshUser(id);
		else {
			TopPanel.getInstance().load(instance.user);
			ProfileView.getInstance().load(instance.user);
		}
	}

	public static ProfileListener getInstance() {
		if (instance == null)
			instance = new ProfileListener();
		return instance;
	}

	public void refreshUser(final int id) {
		baseService.updateUser(id, new AsyncCallback<User>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(User result) {
				if (result == null)
					Window.Location.replace(Host.getPAGE_NOT_FOUND());
				if (id == -1) {
					instance.user = result;
					getUser(-1);
				}
			}
		});
	}

	public void modifyPassword(String oldPassword, String newPpassword) {
		service.modifyPassword(oldPassword, newPpassword, new AsyncCallback<Boolean>(){

			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				ProfileView.getInstance().passwordLoadImage.setVisible(false);
				if(result)
					ProfileView.getInstance().passwordMessage.setText("修改成功");
				else
					ProfileView.getInstance().passwordMessage.setText("密码错误");
			}
			
		});
	}

	public void modifyRegName(String regName){
		service.modifyRegName(regName, new AsyncCallback<Boolean>(){

			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				ProfileView.getInstance().regNameLoadImage.setVisible(false);
				if(result)
					refreshUser(-1);
				else
					ProfileView.getInstance().regNameMessage.setText("用户名不超过16个字符,不能包含标点符号");
			}
			
		});
	}
	
	public void modifyRealName(String realName){
		service.modifyRealName(realName, new AsyncCallback<Boolean>(){
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}
			@Override
			public void onSuccess(Boolean result) {
				ProfileView.getInstance().realNameLoadImage.setVisible(false);
				if(result)
					refreshUser(-1);
				else
					ProfileView.getInstance().realNameMessage.setText("用户名不超过16个字符,不能包含标点符号");
			}
		});
	}
	
	public void modifyAddress(String address){
		service.modifyAddress(address, new AsyncCallback<Boolean>(){
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}
			@Override
			public void onSuccess(Boolean result) {
				ProfileView.getInstance().addressLoadImage.setVisible(false);
				if(result)
					refreshUser(-1);
				else
					ProfileView.getInstance().addressMessage.setText("超过长度限制");
			}
		});
	}
	
	public void modifyIntroduction(String introduction){
		service.modifyIntroduction(introduction, new AsyncCallback<Boolean>(){
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				ProfileView.getInstance().introductionLoadImage.setVisible(false);
				if(result)
					refreshUser(-1);
				else
					ProfileView.getInstance().introductionMessage.setText("超过长度限制800字");
			}
		});
	}
	
	public void modifyBlog(String blog){
		service.modifyBlog(blog, new AsyncCallback<Boolean>(){
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				ProfileView.getInstance().blogLoadImage.setVisible(false);
				if(result)
					refreshUser(-1);
				else
					ProfileView.getInstance().blogMessage.setText("超过长度限制");
			}
		});
	}
	
}
